package com.kll.carolinehmibiz.presentation.presenter

import android.content.Context
import com.kll.carolinehmibiz.OperationManager
import com.kll.carolinehmibiz.model.OperateListener
import com.kll.carolinehmibiz.net.bean.AddPhoto
import com.kll.carolinehmibiz.net.bean.GetPhotoAlbumResponse
import com.kll.carolinehmibiz.presentation.BasePresenter
import com.kll.carolinehmibiz.presentation.listener.PhotoAlbumLovelyProtocol

class PhotoAlbumLovelyPresenter(context: Context, mView: PhotoAlbumLovelyProtocol.IView) : BasePresenter(context), PhotoAlbumLovelyProtocol.IPresenter {

    private var mView: PhotoAlbumLovelyProtocol.IView? = mView
    private var getPhotoIsBusying = false
    private var addPhotoIsBusying = false
    private var delPhotoIsBusying = false

    override fun onDismiss() {
        super.onDismiss()
        mView = null
    }

    override fun loadPhotoDatas(isFristPage: Boolean) {
        if (getPhotoIsBusying) return
        getPhotoIsBusying = true
        OperationManager.getInstancce().getAlbum(isFristPage, object : OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                getPhotoIsBusying = false
                val data = obj as GetPhotoAlbumResponse
                mView?.loadPhotoDataSuccessed(data.list!!)
            }

            override fun onFail(code: Int, message: String) {
                getPhotoIsBusying = false
                mView?.loadPhotoDataFailed(code, message)
            }

        })
    }

    override fun addPhoto(list: ArrayList<AddPhoto.Photo>) {
        if (addPhotoIsBusying) return
        addPhotoIsBusying = true
        OperationManager.getInstancce().addPhoto(list, object : OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                addPhotoIsBusying = false
                if (state == 200) {
                    mView?.addPhotoSuccessed()
                }
            }

            override fun onFail(code: Int, message: String) {
                addPhotoIsBusying = false
                mView?.addPhotoFailed(code, message)
            }

        })
    }

    override fun delPhoto(list: ArrayList<Int>) {
        if (delPhotoIsBusying) return
        delPhotoIsBusying = true
        OperationManager.getInstancce().delPhoto(list, object : OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                delPhotoIsBusying = false
                if (state == 200) {
                    mView?.delPhotoSuccessed()
                }
            }

            override fun onFail(code: Int, message: String) {
                delPhotoIsBusying = false
                mView?.delPhotoFailed(code, message)
            }

        })
    }


}